package com.xyzq.cgate.demo.http.web;


import com.xyzq.cgate.demo.http.entity.User;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.reactive.function.client.ClientResponse;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.Disposable;
import reactor.core.publisher.Mono;

/**
 * @Description:
 * @author chenchen
 * @date 2019/6/19 10:31
 */
@RestController
@RequestMapping("/http/test")
public class HttpTestController {

    @GetMapping("get")
    public Mono<Void> get() throws Exception {
        WebClient WEB_CLIENT = WebClient.create();
        Mono<ClientResponse> exchange = WEB_CLIENT
                .get()
                .uri("http://192.20.63.54:8001/http/test/get")
                .exchange();
        System.out.println(4444);
        ClientResponse clientResponse = exchange.block();
        HttpStatus httpStatus = clientResponse.statusCode();
        if (httpStatus.is2xxSuccessful()) {
            System.out.println(11111);
        } else {
            throw new Exception(httpStatus.getReasonPhrase());
        }
        return Mono.empty();
    }


    @GetMapping("get6")
    public Mono<Void> get6() {
        Mono<ClientResponse> resp = WebClient.create().get()
                .uri("http://192.20.63.54:8001/http/test/get")
                .exchange();
        ClientResponse response = resp.block();
        HttpStatus httpStatus = response.statusCode();
        return Mono.empty();
    }


    public  Mono<String> get3() {
        WebClient WEB_CLIENT = WebClient.create();
        Mono<String> stringMono = WEB_CLIENT
                .get()
                .uri("http://192.20.63.54:8001/http/test/get")
                .exchange()
                .flatMap(clientResponse -> {
                    System.out.println(clientResponse.bodyToMono(String.class));
                    return clientResponse.bodyToMono(String.class);
                });
        return stringMono;

    }

    @GetMapping("post")
    public Mono<User> post(@RequestBody User user) {
        return Mono.just(user);
    }

    private void dealError(Throwable e) {
        e.printStackTrace();

    }

}
